package com.genghis.ptas.posts.service.impl;

import com.genghis.core.page.entity.Page;
import com.genghis.core.page.entity.PageParameter;
import com.genghis.ptas.postreply.dao.PostReplyDao;
import com.genghis.ptas.posts.dao.PostsDao;
import com.genghis.ptas.posts.entity.Posts;
import com.genghis.ptas.posts.service.PostsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Created with IntelliJ IDEA.
 * User: yutong
 * Date: 14-4-22
 * Time: 下午8:16
 * To change this template use File | Settings | File Templates.
 */
@Transactional
@Service("postsService")
public class PostsServiceImpl implements PostsService {

    @Autowired
    private PostsDao postsDao;

    @Autowired
    private PostReplyDao postReplyDao;

    @Override
    public void addPosts(Posts posts) {
         postsDao.addPosts(posts);
    }

    @Override
    public Page<Posts> findPostsPageList(PageParameter pageParameter, Posts posts) {
        Page<Posts> postsPage =postsDao.findPostsPageList(pageParameter,posts);
        for(Posts postsPage1 :postsPage.getResultList()){
            postsPage1.setReplyNum(postsDao.findReplyNumById(postsPage1.getId()));
        }
        return postsPage;
    }

    @Override
    public Posts findPostById(int id) {
        return postsDao.findPostById(id);
    }

    @Override
    public void delPosts(String[] postsIds) {
        for(String postsId:postsIds){
             postsDao.delPosts(Integer.parseInt(postsId));
             postReplyDao.delAllReply(Integer.parseInt(postsId));
        }
    }
}
